﻿Imports System.Data.OleDb

Public Class UserProfiles
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM UserProfile  WHERE UserID=@7"
            oledbConn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oledbConn)
            cmd.Parameters.AddWithValue("@f7", User.Identity.Name)

            Dim dr = cmd.ExecuteReader
            dr.Read()

            If Not IsDBNull(dr("FirstName")) Then tb_FName.Text = dr("FirstName")
            If Not IsDBNull(dr("LastName")) Then tb_LName.Text = dr("LastName")
            If Not IsDBNull(dr("Gender")) Then ddl_Gender.Text = dr("Gender")
            If Not IsDBNull(dr("EmailAddress")) Then tb_Email.Text = dr("EmailAddress")

            If Not IsDBNull(dr("DateOfBirth")) Then
                Dim userDOB As DateTime = dr("DateOfBirth")
                ddl_Year.SelectedValue = userDOB.Year
                ddl_Month.SelectedValue = userDOB.Month
                ddl_Day.SelectedValue = userDOB.Day
            End If
        End If
    End Sub

    Protected Sub Btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Submit.Click

        Dim newFilename As String = ""
        If fup_Portrait.HasFile Then
            newFilename = Guid.NewGuid.ToString() & getSuffix(fup_Portrait.FileName)
            If fup_Portrait.HasFile Then
                fup_Portrait.SaveAs(Server.MapPath("Pictures") & "/" & newFilename)
            End If
        End If
        Dim userDOB As DateTime = New DateTime(CInt(ddl_Year.SelectedValue), (ddl_Month.SelectedValue), (ddl_Day.SelectedValue))

        Dim SqlString As String
        If fup_Portrait.HasFile Then

            SqlString = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Portrait=@f3, Gender=@f4, DateOfBirth=@f5, EmailAddress=@f6  WHERE UserID=@7"
        Else
            SqlString = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Gender=@f4, DateOfBirth=@f5, EmailAddress=@f6  WHERE UserID=@7"
        End If

        Dim oledbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("JDFanConnectionString").ConnectionString)
        oledbConn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oledbConn)

        cmd.Parameters.AddWithValue("@f1", tb_FName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LName.Text)
        If fup_Portrait.HasFile Then cmd.Parameters.AddWithValue("@f3", "Pictures/" & newFilename)
        cmd.Parameters.AddWithValue("@f4", ddl_Gender.Text)
        cmd.Parameters.AddWithValue("@f5", userDOB.Date)
        cmd.Parameters.AddWithValue("@f6", tb_Email.Text)
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)

        cmd.ExecuteNonQuery()
        oledbConn.Close()
        cmd.Dispose()
        oledbConn.Dispose()


    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class